﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ERP.Classes
{
    public class cs_erp_monanInfo
    {
        private int _id_monan;
        private string _maql;
        private string _tenmon;
        private int _manhom;
        private double _dongia;
        private string _ghichu;
        private int _donvitinh;
        private int _trangthai;

        public cs_erp_monanInfo(int id_monan, string maql, string tenmon, int manhom, double dongia, string ghichu, int donvitinh, int trangthai)
        {
            this._id_monan = id_monan;
            this._maql = maql;
            this._tenmon = tenmon;
            this._manhom = manhom;
            this._dongia = dongia;
            this._ghichu = ghichu;
            this._donvitinh = donvitinh;
            this._trangthai = trangthai;

        }
        public cs_erp_monanInfo()
        {
            this._id_monan = 0;
            this._maql = " ";
            this._tenmon = " ";
            this._manhom = 0;
            this._dongia = 0;
            this._ghichu = " ";
            this._donvitinh = 0;
            this._trangthai = 0;

        }
        public int id_monan
        {
            get
            {
                return this._id_monan;
            }
            set
            {
                this._id_monan = value;
            }
        }
        public string maql
        {
            get
            {
                return this._maql;
            }
            set
            {
                this._maql = value;
            }
        }
        public string tenmon
        {
            get
            {
                return this._tenmon;
            }
            set
            {
                this._tenmon = value;
            }
        }
        public int manhom
        {
            get
            {
                return this._manhom;
            }
            set
            {
                this._manhom = value;
            }
        }
        public double dongia
        {
            get
            {
                return this._dongia;
            }
            set
            {
                this._dongia = value;
            }
        }
        public string ghichu
        {
            get
            {
                return this._ghichu;
            }
            set
            {
                this._ghichu = value;
            }
        }
        public int donvitinh
        {
            get
            {
                return this._donvitinh;
            }
            set
            {
                this._donvitinh = value;
            }
        }
        public int trangthai
        {
            get
            {
                return this._trangthai;
            }
            set
            {
                this._trangthai = value;
            }
        }

        public int ExcuteStoreProcedure(string storeProcedure, int option)
        {
            string[] paraName = new string[9];
            paraName[0] = "@id_monan";
            paraName[1] = "@maql";
            paraName[2] = "@tenmon";
            paraName[3] = "@manhom";
            paraName[4] = "@dongia";
            paraName[5] = "@ghichu";
            paraName[6] = "@donvitinh";
            paraName[7] = "@trangthai";
            paraName[8] = "@option";
            object[] paraValue = new object[9];
            paraValue[0] = this._id_monan;
            paraValue[1] = this._maql;
            paraValue[2] = this._tenmon;
            paraValue[3] = this._manhom;
            paraValue[4] = this._dongia;
            paraValue[5] = this._ghichu;
            paraValue[6] = this._donvitinh;
            paraValue[7] = this._trangthai;
            paraValue[8] = option;
            csCSDL csdl = new csCSDL();
            int i = 0;
            i = csdl.CreateParameters(paraName, 9, paraValue);
            return csdl.ExcuteStoreProcedure(storeProcedure);
        }
        
        public int ExcuteProcedure(int option)
        {
            return this.ExcuteStoreProcedure("Proc_ERP_MONAN", option);
        }

        public int Get_Info()
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                ds = csdl.OpenTable("ERP_MONAN", "id_monan = " + this._id_monan);
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    return 0;
                }
                this._id_monan = Convert.ToInt32(ds.Tables[0].Rows[0]["id_monan"].ToString().Trim());
                this._maql = ds.Tables[0].Rows[0]["maql"].ToString().Trim();
                this._tenmon = ds.Tables[0].Rows[0]["tenmon"].ToString().Trim();
                this._manhom = Convert.ToInt32(ds.Tables[0].Rows[0]["manhom"].ToString().Trim());
                this._dongia = Convert.ToDouble(ds.Tables[0].Rows[0]["dongia"].ToString().Trim());
                this._ghichu = ds.Tables[0].Rows[0]["ghichu"].ToString().Trim();
                this._donvitinh = Convert.ToInt32(ds.Tables[0].Rows[0]["donvitinh"].ToString().Trim());
                this._trangthai = Convert.ToInt32(ds.Tables[0].Rows[0]["trangthai"].ToString().Trim());
                return 1;
            }
            catch
            {
                return -1;
            }
        }

        public DataSet Get_List()
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                string sql = "select a.id_monan, a.maql, a.tenmon, b.tennhom, a.dongia, a.ghichu, c.donvitinh"
                + " from ERP_MONAN a, ERP_NHOMMONAN b, ERP_DVT c"
                + " where a.manhom = b.id_nhom and a.donvitinh = c.id_dvt and a.trangthai = 1"
                + " order by manhom asc";
                ds = csdl.SelecSQL(sql);

                return ds;
            }
            catch
            {
                return null;
            }
        }

        public DataSet Get_List_Mon()
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                string sql = "select a.id_monan, a.maql, a.tenmon, b.tennhom, a.dongia, a.ghichu, c.donvitinh, 0 as soluong"
                + " from ERP_MONAN a, ERP_NHOMMONAN b, ERP_DVT c"
                + " where a.manhom = b.id_nhom and a.donvitinh = c.id_dvt and a.trangthai = 1"
                + " order by manhom asc";
                ds = csdl.SelecSQL(sql);

                return ds;
            }
            catch
            {
                return null;
            }
        }

        public DataSet Get_List_Mon_Dieuchinh(cs_erp_hoadonInfo hd)
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                string sql = "select a.id_monan, a.maql, a.tenmon, b.tennhom, a.dongia, a.ghichu, c.donvitinh, d.soluong"
                + " from ERP_MONAN a"
                + " left join ERP_CHITIET_HOADON d on (d.id_monan = a.id_monan and d.id_hoadon = " + hd.id_hd + ")"
                + " left join ERP_HOADON e on (e.id_hd = d.id_hoadon and e.trangthai = " + hd.trangthai + " and e.id_ban = " + hd.id_ban + ")"
                + " left join ERP_NHOMMONAN b on (b.id_nhom = a.manhom)"
                + " left join ERP_DVT c on (a.donvitinh = c.id_dvt)"
                + " where a.trangthai = 1"
                + " order by manhom asc";
                ds = csdl.SelecSQL(sql);

                return ds;
            }
            catch
            {
                return null;
            }
        }

    }
}
